package servlet;

import dao.DaoFactory;
import entity.Exam;
import entity.Teacher;
import utils.PageInfo;
import utils.PathUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.sql.SQLException;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {

    private static final long serialVersionUID = -6391063958039740500L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        System.out.println(method);
        if("list".equals(method)) {//老师列出所有考试
            this.list(req, resp);
        }else if("add".equals(method)) {//老师添加考试
            this.add(req, resp);
        }else if("update".equals(method)) {//修改老师信息
            this.update(req, resp);
        }else if("delete".equals(method)) {//老师删除考试
            this.delete(req, resp);
        }
    }

    public Integer getIntParameter(HttpServletRequest request,String name) {
        if(StringUtils.isNoneBlank(request.getParameter(name))) {
            return Integer.parseInt(request.getParameter(name));
        }else {
            return null;
        }
    }

    //老师添加考试
    private void add(HttpServletRequest request, HttpServletResponse response) {//js已经验证不为空
        String t_id = request.getParameter("t_id");
        String e_name = request.getParameter("e_name");
        String e_starttime = request.getParameter("e_starttime");
        String e_stoptime = request.getParameter("e_stoptime");
        String e_autostartS = request.getParameter("e_autostart");
        Integer e_autostart = 0;

        if(e_autostartS==null) {
            e_autostart = 0;
        }else {
            e_autostart=1;
        }

        Exam exam = new Exam();
        exam.setT_id(t_id);
        exam.setE_name(e_name);
        exam.setE_starttime(e_starttime);
        exam.setE_stoptime(e_stoptime);
        exam.setE_autostart(e_autostart);
        exam.setE_status(0);
        try {
            DaoFactory.getInstance().getExamDao().add(exam);
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"teacher?method=list&msg=Add exam sucessful!");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

     //老师删除考试
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer e_id = getIntParameter(request,"e_id");
        try {
            DaoFactory.getInstance().getExamDao().deleteExam(e_id);
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"teacher?method=list&msg=Delete exam sucessful!");
        } catch (Exception e) {

            e.printStackTrace();
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"teacher?method=list&msg=Sorry,you cannot. Has students exist.");

        }
    }


    //修改老师信息
    private void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("update");
        String t_id = request.getParameter("t_id");
        String t_name = request.getParameter("t_name");
        Integer t_isadmin = getIntParameter(request,"t_isadmin");
        Teacher teacher = new Teacher();
        teacher.setT_id(t_id);
        teacher.setT_isadmin(t_isadmin);
        teacher.setT_name(t_name);

        try {
            DaoFactory.getInstance().getTeacherDao().update(teacher);
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"admin?method=list&msg=Update sucessful!");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据老师id，列出他创建的考试
    private void list(HttpServletRequest request, HttpServletResponse response) {
        //当前页码
        Integer pageNo = getIntParameter(request, "pageNo");
        //查询条件
        String e_name = request.getParameter("e_name");
        String msg = request.getParameter("msg");
        Teacher teacher = (Teacher) request.getSession().getAttribute("user");
        String t_id = teacher.getT_id();
        //构造了一个pageInfo对象
        PageInfo<Exam> pageInfo = new PageInfo<>(pageNo);
        try {
            pageInfo = DaoFactory.getInstance().getExamDao().listExam(t_id,e_name,pageInfo);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {

            request.setAttribute("pageInfo", pageInfo);
            //回写到页面
            request.getRequestDispatcher("page/Teacher/listExam.jsp?msg="+msg).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
